<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="品类等级" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入品类等级"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"  v-hasPermi="['system:teamCategoryPrice:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
          <el-radio-group v-model="queryParams.type" @change="handleQuery">
            <el-radio-button :label="1">普通品类</el-radio-button>
            <el-radio-button :label="2">上分品类</el-radio-button>
          </el-radio-group>
      </el-col>
    </el-row>

    
    <el-table v-loading="loading" :data="teamCategoryPriceList">
      <el-table-column label="品类名称" align="center" prop="itemId" >
        <template slot-scope="scope">
          <span v-for="item in teamCategoryOptions" :key="item.id">
            <span v-if="item.id === scope.row.itemId">{{item.name}}</span>
          </span>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column :label="queryParams.type===1?'品类等级':'游戏段位'" align="center" prop="level" />
      <el-table-column label="价格(￥)" align="center" prop="price">
         <template slot-scope="scope">
          <span v-for="item in teamCategoryOptions" :key="item.id">
            <span v-if="item.id === scope.row.itemId">{{scope.row.price +'/'+unitFormat(item.tollType) }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="享受折扣" align="center" prop="discount">
        <template slot-scope="scope">
          <span v-if="scope.row.discount && scope.row.discount.length">
            <span v-for="item in scope.row.discount" :key="item">
              {{item+'折,'}}
            </span>
          </span>
          <span v-else>
            无
          </span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teamCategoryPrice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teamCategoryPrice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改品类价格设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="品类类别" prop="type">
          <el-radio-group v-model="form.type" >
            <el-radio :label="1">普通</el-radio>
            <el-radio :label="2">上分</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="品类选择" prop="itemId">
          <el-select v-model="form.itemId" placeholder="请选择品类类别">
            <el-option v-for="item in teamCategoryOptions" :key="item.id" :label="item.teamCategoryName +' > '+item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.type===1" label="等级" prop="level">
          <el-select v-model="form.level" placeholder="请选择等级">
            <el-option v-for="item in itemLevelOptions" :key="item.id" :label="item.godName" :value="item.godName" />
          </el-select>
        </el-form-item>
        <el-form-item v-else label="游戏段位" prop="level">
          <el-input v-model="form.level" placeholder="请输入游戏段位" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="form.price" placeholder="请输入价格" :controls="false"/>
        </el-form-item>
        <el-form-item label="享受折扣" prop="discount">
          <el-select v-model="form.discount" placeholder="请选择折扣" multiple>
            <el-option label="1折" :value="1" />
            <el-option label="2折" :value="2" />
            <el-option label="3折" :value="3" />
            <el-option label="4折" :value="4" />
            <el-option label="5折" :value="5" />
            <el-option label="6折" :value="6" />
            <el-option label="7折" :value="7" />
            <el-option label="8折" :value="8" />
            <el-option label="9折" :value="9" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeamCategoryPrice, getTeamCategoryPrice, delTeamCategoryPrice, addTeamCategoryPrice, updateTeamCategoryPrice, exportTeamCategoryPrice } from "@/api/system/teamCategoryPrice";
import { loadTeamCategoryItem } from "@/api/system/teamCategoryItem";
import { loadLevel } from "@/api/system/level";
export default {
  name: "TeamCategoryPrice",
  data() {
    return {
      teamCategoryOptions:[],
      itemLevelOptions:[],
      unitOptions:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 品类价格设置表格数据
      teamCategoryPriceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: undefined,
        type: 1,
        level: undefined,
        price: undefined,
        discount: undefined,
        createDate: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemId: [
          { required: true, message: "品类ID不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "品类类别不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    loadTeamCategoryItem().then(response => {
      this.teamCategoryOptions = response.data;
    });
    this.getDicts("charging_method").then(response => {
      this.unitOptions = response.data;
    });
    loadLevel().then(response => {
      this.itemLevelOptions = response.data;
    });
  },
  methods: {
    // 礼物位置字典翻译
    unitFormat(key) {
      let v = ''
      this.unitOptions.forEach(e => {
        if(e.dictValue === key){
          v = e.dictLabel
        }
      })
      return v
    },
    /** 查询品类价格设置列表 */
    getList() {
      this.loading = true;
      listTeamCategoryPrice(this.queryParams).then(response => {
        this.teamCategoryPriceList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        itemId: undefined,
        type: undefined,
        level: undefined,
        price: undefined,
        discount: undefined,
        createDate: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品类价格设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeamCategoryPrice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改品类价格设置";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      console.log('submitForm = %o', this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTeamCategoryPrice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addTeamCategoryPrice(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除品类价格设置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTeamCategoryPrice(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有品类价格设置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTeamCategoryPrice(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>